#include <Windows.h>
#include <tchar.h>
#include "resource.h"

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	static bool bAnsi = false; // unicode
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0113"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			EnableWindow(GetDlgItem(hDlg, IDC_BTNSAVE), FALSE);
			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNOPEN:
				{
					TCHAR szFilePath[MAX_PATH] = {0};
					OPENFILENAME ofn= {0};
					ofn.lStructSize = sizeof(ofn);
					ofn.hwndOwner	= hDlg;
					ofn.Flags		= OFN_EXPLORER;
					ofn.lpstrFilter	= _T("txt|*.txt");
					ofn.lpstrFile	= szFilePath;
					ofn.nMaxFile	= _countof(szFilePath);
					if (GetOpenFileName(&ofn))
					{
						SetDlgItemText(hDlg, IDC_STCPATH, szFilePath);
						HANDLE hFile = CreateFile(szFilePath, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ,
							                      NULL, OPEN_EXISTING, NULL, NULL);
						if (INVALID_HANDLE_VALUE != hFile)
						{
							DWORD dwFileSize = GetFileSize(hFile, NULL);
							HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); 
							if (NULL != hFileMapping)
							{
								int nResult = -1;
								PSTR pContent = (PSTR)MapViewOfFile(hFileMapping, FILE_MAP_WRITE|FILE_MAP_READ, 0, 0, 0);
								if (IsTextUnicode(pContent, dwFileSize, &nResult))
								{
									bAnsi = false;
									SetDlgItemText(hDlg, IDC_EDTCONTENT, (LPTSTR)pContent);
								} else {
									bAnsi = true;
									TCHAR* szBuffer = new TCHAR[dwFileSize];
									mbstowcs(szBuffer, pContent, dwFileSize);
									SetDlgItemText(hDlg, IDC_EDTCONTENT, (LPTSTR)szBuffer);
									delete[] szBuffer;
								}
								
								UnmapViewOfFile(pContent);
								CloseHandle(hFileMapping);
								CloseHandle(hFile);

								EnableWindow(GetDlgItem(hDlg, IDC_BTNSAVE), TRUE);
							}
						}
					}

					break;
				}
				case IDC_BTNSAVE:
				{
					TCHAR* szBuffer = NULL;
					__try {
						DWORD dwLen = SendMessage(GetDlgItem(hDlg, IDC_EDTCONTENT), WM_GETTEXTLENGTH, 0, 0);
						szBuffer = new TCHAR[dwLen];
						GetDlgItemText(hDlg, IDC_EDTCONTENT, (LPTSTR)szBuffer, dwLen);

						TCHAR szFilePath[MAX_PATH];
						GetDlgItemText(hDlg, IDC_STCPATH, szFilePath, _countof(szFilePath));
						HANDLE hFile = CreateFile(szFilePath, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ,
												  NULL, OPEN_EXISTING, NULL, NULL);
						if (INVALID_HANDLE_VALUE != hFile)
						{
							DWORD dwFileSize = bAnsi ? dwLen : sizeof(TCHAR) * dwLen;
							HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwFileSize, NULL); 
							if (NULL != hFileMapping)
							{
								int nResult = -1;
								PSTR pContent = (PSTR)MapViewOfFile(hFileMapping, FILE_MAP_WRITE|FILE_MAP_READ, 0, 0, 0);
												
								if (bAnsi)
								{
									wcstombs(pContent, szBuffer, dwFileSize);
								} else {
									CopyMemory(pContent, szBuffer, dwFileSize);
								}
								FlushViewOfFile(pContent, 0);
								UnmapViewOfFile(pContent);
								SetFilePointer(hFile, dwFileSize, 0, FILE_BEGIN);
								SetEndOfFile(hFile);
								CloseHandle(hFileMapping);
								CloseHandle(hFile);
							}
						}
					} __finally {
						if (NULL != szBuffer)
						{
							delete[] szBuffer;
						}
					}


					break;
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}